//
//  SYMyGroupViewController.m
//  BSHOUYIFANG
//
//  Created by lanou3g on 15/8/21.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYMyGroupViewController.h"
#import "Header.h"
#import "SYMomentsTypeTableViewCell.h"
#import "SYMomentsTypeModel.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#import "SYMyHomeGroupModel.h"
#import "SYMomentsFirstTableViewController.h"
#import "SYFileHandle.h"

@interface SYMyGroupViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *mainTV;
@property (nonatomic,strong)FMDatabase *db;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)SYMyHomeGroupModel *model;
@end

@implementation SYMyGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationItemTitleColor];
    _dataArr = [NSMutableArray arrayWithCapacity:0];
    _model = [[SYMyHomeGroupModel alloc]init];
    _model.typeModel = [[SYMomentsTypeModel alloc]init];
    _mainTV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _mainTV.dataSource = self;
    _mainTV.delegate = self;
    [self.view addSubview:_mainTV];
    _mainTV.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:1];
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [str stringByAppendingPathComponent:@"like.db"];
    _db = [FMDatabase databaseWithPath:filePath];
    if (![_db open]) {
        NSLog(@"%@",filePath);
    }
    NSLog(@"%@",filePath);

    [self parserData];
    UIButton*leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30*0.6,30*0.6)];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"]forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(searchprogram) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem= leftItem;

}

- (void)searchprogram{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)navigationItemTitleColor
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"我的圈子"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = customLab;
}
   //*************读取数据库*************//

- (void)parserData{
    [_dataArr removeAllObjects];
    _model.typeModel.quan_name = @"";
    NSString *userName = [[SYFileHandle shareInstance] phoneNumber];
    FMResultSet *result = [_db executeQuery:@"select title, imgstr, backupstr, name, count from quanzi where username = ?",userName];
    while ([result next]) {
        NSString *title = [result stringForColumn:@"title"];
        NSString *imstr = [result stringForColumn:@"imgstr"];
        NSString *backupStr = [result stringForColumn:@"backupstr"];
        NSString *name = [result stringForColumn:@"name"];
        NSInteger count = [result intForColumn:@"count"];
        SYMyHomeGroupModel *model = [[SYMyHomeGroupModel alloc]init];
        model.typeModel = [[SYMomentsTypeModel alloc]init];
        if ([name isEqualToString:_model.typeModel.quan_name]) {
            continue;
        }
        model.typeModel.quan_name = name;
        model.typeModel.quan_slogan = title;
        model.typeModel.quan_logo = imstr;
        model.typeModel.new_say_count = count;
        model.backupStr = backupStr;
        _model.typeModel.quan_name = name;
        [_dataArr addObject:model];
    }
    [result close];
    for (int i = 0; i < _dataArr.count; i++) {
        SYMyHomeGroupModel *model = _dataArr[i];
        FMResultSet *set = [_db executeQuery:@"select fans from quanzi where title = ? and username = ?",model.typeModel.quan_slogan,userName];
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        while ([set next]) {
            NSString *fans = [set stringForColumn:@"fans"];
            NSRange range = [fans rangeOfString:@"http:"];
            NSRange range2 = [fans rangeOfString:@".jpg"];
            NSString *icon = [fans substringWithRange:NSMakeRange(range.location, range2.location + range2.length - range.location)];
            NSDictionary *dict = @{@"user_face":icon};
            [arr addObject:dict];
        }
        model.typeModel.quan_fans = arr;
        [_dataArr replaceObjectAtIndex:i withObject:model];
        [set close];
    }
    [_mainTV reloadData];
}


   //*************tableview datasource*************//

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SYMomentsTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[SYMomentsTypeTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    SYMyHomeGroupModel *model = _dataArr[indexPath.row];
    cell.model = model.typeModel;
    return cell;
}

   //*************点击跳转事件*************//

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SYMomentsFirstTableViewController *fstVC = [[SYMomentsFirstTableViewController alloc]init];
    SYMyHomeGroupModel *model = _dataArr[indexPath.row];
    fstVC.typeModel = model.typeModel;
    fstVC.backupStr = model.backupStr;
    fstVC.naviTitle = model.typeModel.quan_name;
    fstVC.jumpIndex = 1;
    [self.navigationController pushViewController:fstVC animated:YES];
}

   //*************实时更新数据*************//

- (void)viewWillAppear:(BOOL)animated{
    [_dataArr removeAllObjects];
    [self parserData];
    [_mainTV reloadData];
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
